using System;
using System.ComponentModel;
using System.Windows.Forms;
using Company.BLL.SXXK;
using Company.BLL.DuLieuChuan;
using Company.Interface.KDT.SXXK;
using Janus.Windows.GridEX;
using Company.BLL;

namespace Company.Interface.KDT.SXXK
{
    public partial class NguyenPhuLieuForm : BaseForm
    {
        public NguyenPhuLieuForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            dgList.DataSource = new NguyenPhuLieu().SelectDynamic("IsRegisted = 0", "Ma").Tables[0];
        }

        //-----------------------------------------------------------------------------------------

        private void NguyenPhuLieu_Form_Load(object sender, EventArgs e)
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();
            cbDonViTinh.DataSource = this._DonViTinh;
            cbDonViTinh.DisplayMember = "Ten";
            cbDonViTinh.ValueMember = "ID";
            // Nguyên phụ liệu.
            this.BindData();

            dgList.Tables[0].Columns["MaDoanhNghiep"].Visible = GlobalSettings.DAI_LY_TTHQ;
        }
        //-----------------------------------------------------------------------------------------

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            //cvError.Validate();
            //if (!cvError.IsValid) return;
            //NguyenPhuLieu npl = new NguyenPhuLieu();
            //npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            //npl.Ma = txtMa.Text;
            //npl.Ten = txtTen.Text;
            //npl.MaHS = txtMaHS.Text;
            //npl.DVT_ID = cbDonViTinh.SelectedValue.ToString();
            ////npl.IsRegisted = false;
            ////npl.CreatedDate = DateTime.Now;
            //if (!npl.IsExist)
            //{
            //    int ret = npl.Insert();
            //    if (ret > 0)
            //    {
            //        this.BindData();
            //        txtMa.Text = string.Empty;
            //        txtTen.Text = string.Empty;
            //        txtMaHS.Text = string.Empty;
            //        txtMa.Select();
            //    }
            //    else
            //    {
            //        MessageBox.Show("Thao tác không thành công");
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("Nguyên phụ liệu này đã có trong danh mục.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    txtMa.Select();
            //}
        }

        //-----------------------------------------------------------------------------------------
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            //GridEXSelectedItemCollection items = dgList.SelectedItems;
            //foreach (GridEXSelectedItem i in items)
            //{
            //    if (i.RowType == RowType.Record)
            //    {
            //        NguyenPhuLieuEditForm f = new NguyenPhuLieuEditForm();
            //        f.NPLDetail.Ma = i.GetRow().Cells["Ma"].Text;
            //        f.NPL.MaDoanhNghiep = i.GetRow().Cells["MaDoanhNghiep"].Text;
            //        f.NPL.Load();
            //        f.ShowDialog();
            //        if (f.IsEdited)
            //        {
            //            f.NPL.Update();
            //        }
            //        else if (f.IsDeleted)
            //        {
            //            f.NPL.Delete();
            //        }
            //    }
            //    break;
            //}
            //this.BindData();
        }

        //-----------------------------------------------------------------------------------------
        private void dgList_DeletingRecords(object sender, CancelEventArgs e)
        {
            if (MessageBox.Show("Có muốn xóa các nguyên phụ liệu này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                GridEXSelectedItemCollection items = dgList.SelectedItems;
                NguyenPhuLieuCollection collection = new NguyenPhuLieuCollection();
                foreach (GridEXSelectedItem i in items)
                {
                    if (i.RowType == RowType.Record)
                    {
                        NguyenPhuLieu npl = new NguyenPhuLieu();
                        npl.Ma = i.GetRow().Cells["Ma"].Text;
                        npl.MaDoanhNghiep = i.GetRow().Cells["MaDoanhNghiep"].Text;
                        collection.Add(npl);
                    }
                }
                bool ret = new NguyenPhuLieu().DeleteCollection(collection);
                if (ret)
                {
                    MessageBox.Show("Thao tác thành công");
                }
                else
                {
                    MessageBox.Show("Thao tác không thành công");
                }
            }
            else
            {
                e.Cancel = true;
            }
        }

        //-----------------------------------------------------------------------------------------
        private void btnMaHS_Click(object sender, EventArgs e)
        {
            //DanhMucHS_Form f = new DanhMucHS_Form();
            //f.ShowDialog();
            //if (f.IsChose)
            //{
            //    txtMaHS.Text = f.MaHS;
            //}
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
                e.Row.Cells["DVT_Ten"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Value.ToString());
        }

        //-----------------------------------------------------------------------------------------
    }
}